import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;

import javax.swing.JPasswordField;

import java.io.IOException;
import java.util.logging.*;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
/**
 * This is the class of the GUI that the user sees when logging in.
 */
public class LoginPanel extends JPanel implements ActionListener{
	private JTextField txtUsername;

	private JPasswordField pwdPassword;
	
	private JLabel loginEventLbl;
	
	private JLabel loginEventLbl2;
	
	private static Logger loginPanelLog = Logger.getLogger(LoginPanel.class.getName());
	
	private User currentUser;
	
	static{
		ConsoleHandler console = new ConsoleHandler();
		console.setLevel(Level.FINEST);
		console.setFormatter(new ThunderDragonHtmlFormatter());
		loginPanelLog.addHandler(console);
		loginPanelLog.setLevel(Level.ALL);
		
		try {
			FileHandler fh = new FileHandler("LoginPanelLog.txt");
			fh.setFormatter(new ThunderDragonHtmlFormatter());
			loginPanelLog.addHandler(fh);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			loginPanelLog.severe("Failure: Security exception thrown " +
					"when trying to create a new handler" +
					" in LoginPanel.java.");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			loginPanelLog.severe("Failure: I/O exception thrown " +
					"when trying to create a new handler" +
					" in LoginPanel.java.");
			e.printStackTrace();
		}
		
	}

	public LoginPanel() {
		loginPanelLog.entering(this.getClass().getName(), "LoginPanel Constructor");
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		setPreferredSize(new Dimension(500, 327));
		
		txtUsername = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, 
				txtUsername, 85, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, 
				txtUsername, 215, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, 
				txtUsername, -110, SpringLayout.EAST, this);
		txtUsername.setText("Username");
		add(txtUsername);
		txtUsername.setColumns(10);
		txtUsername.addActionListener(this);
		
		JLabel lblUsername = new JLabel("Username:");
		springLayout.putConstraint(SpringLayout.WEST, 
				lblUsername, 65, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH,
				lblUsername, 3, SpringLayout.NORTH, txtUsername);
		springLayout.putConstraint(SpringLayout.EAST,
				lblUsername, -80, SpringLayout.WEST, txtUsername);
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		springLayout.putConstraint(SpringLayout.WEST,
				lblPassword, 0, SpringLayout.WEST, lblUsername);
		springLayout.putConstraint(SpringLayout.EAST,
				lblPassword, 0, SpringLayout.EAST, lblUsername);
		add(lblPassword);
		
		JButton btnLogin = new JButton("LOGIN");
		springLayout.putConstraint(SpringLayout.WEST, 
				btnLogin, 50, SpringLayout.WEST, lblUsername);
		springLayout.putConstraint(SpringLayout.EAST, 
				btnLogin, -75, SpringLayout.EAST, txtUsername);
		btnLogin.setToolTipText("Login to the Hostpital System");
		btnLogin.addActionListener(this);
		add(btnLogin);
		
		pwdPassword = new JPasswordField();
		springLayout.putConstraint(SpringLayout.NORTH, 
				lblPassword, 3, SpringLayout.NORTH, pwdPassword);
		springLayout.putConstraint(SpringLayout.WEST, 
				pwdPassword, 0, SpringLayout.WEST, txtUsername);
		springLayout.putConstraint(SpringLayout.NORTH, 
				btnLogin, 52, SpringLayout.SOUTH, pwdPassword);
		springLayout.putConstraint(SpringLayout.EAST, 
				pwdPassword, 0, SpringLayout.EAST, txtUsername);
		springLayout.putConstraint(SpringLayout.NORTH, 
				pwdPassword, 27, SpringLayout.SOUTH, txtUsername);
		pwdPassword.addActionListener(this);
		add(pwdPassword);
				
		JLabel hospitalLbl = new JLabel("Thunder Dragon Hospital System");
		springLayout.putConstraint(SpringLayout.SOUTH, 
				hospitalLbl, -36, SpringLayout.NORTH, txtUsername);
		springLayout.putConstraint(SpringLayout.NORTH, 
				hospitalLbl, 10, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.EAST, 
				hospitalLbl, -10, SpringLayout.EAST, this);
		hospitalLbl.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.WEST, 
				hospitalLbl, 10, SpringLayout.WEST, this);
		hospitalLbl.setFont(new Font("Pristina", Font.BOLD | Font.ITALIC, 32));
		add(hospitalLbl);
		
		loginEventLbl = new JLabel("");
		springLayout.putConstraint(SpringLayout.NORTH, 
				loginEventLbl, 6, SpringLayout.SOUTH, btnLogin);
		springLayout.putConstraint(SpringLayout.WEST, 
				loginEventLbl, -100, SpringLayout.WEST, btnLogin);
		springLayout.putConstraint(SpringLayout.EAST, 
				loginEventLbl, 100, SpringLayout.EAST, btnLogin);
		loginEventLbl.setForeground(Color.RED);
		loginEventLbl.setFont(new Font("Trebuchet MS", Font.ITALIC, 11));
		loginEventLbl.setHorizontalAlignment(SwingConstants.CENTER);
		add(loginEventLbl);
		
		loginEventLbl2 = new JLabel("");
		springLayout.putConstraint(SpringLayout.NORTH, 
				loginEventLbl2, 6, SpringLayout.SOUTH, loginEventLbl);
		springLayout.putConstraint(SpringLayout.WEST, 
				loginEventLbl2, -10, SpringLayout.WEST, loginEventLbl);
		springLayout.putConstraint(SpringLayout.EAST, 
				loginEventLbl2, 10, SpringLayout.EAST, loginEventLbl);
		loginEventLbl2.setHorizontalAlignment(SwingConstants.CENTER);
		loginEventLbl2.setForeground(Color.RED);
		loginEventLbl2.setFont(new Font("Trebuchet MS", Font.ITALIC, 11));
		add(loginEventLbl2);
		loginPanelLog.exiting(this.getClass().getName(), "LoginPanel Constructor");
	}
	
	/**
	 * @param currentUser the currentUser to set
	 */
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	/**
	 * @return the currentUser
	 */
	public User getCurrentUser() {
		return currentUser;
	}

	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) {
		String result = 
			LoginMech.checkLogin(txtUsername.getText(), pwdPassword.getText());
		if (result.equals("invalid")){
			loginPanelLog.info("Login failed. Invalid username/password.");
			loginEventLbl.setText("Login failed: Invalid username/password");
			loginEventLbl2.setText("Three incorrect guesses will lock an account.");
			
		}
		if (result.equals("success")){
			loginPanelLog.info("Login succeeded.");
			loginEventLbl.setText("");
			loginEventLbl2.setText("");
			setCurrentUser(
					UserDatabase.searchUser(txtUsername.getText(), 
							UserDatabase.USERNAME_SEARCH).get(0));
			txtUsername.setText("");
			pwdPassword.setText("");
			this.setVisible(false);
		}
		if (result.equals("locked")){
			loginPanelLog.info("Login failed, account is locked.");
			loginEventLbl.setText("Login failed: Account locked");
			loginEventLbl2.setText("Contact a System Administrator.");
		}
	}
}


